import {message} from 'antd'

/**
 * 字典接口统一封装
 */

import {postJson} from '@/utils/req'
import {modalApi} from '@/utils/modalReq'

// 获取表格数据
export const getDictsByKeys = (() => (list: {dictKeys: string[]}) => postJson(
			`${modalApi.common}/shopec-systembase/dict/getDictsByKeys`,
			list,
		))()

/**
 * 直接处理字典数据
 */

/**
 * @description: 基于list集合的递归方法
 * @param {string[]} list 集合数组
 */

interface Callback {
	(name: string): void
}
export function getDict(list: {dictKeys: string[]}, callback: Callback) {
	getDictsByKeys(list)
		.then((res) => {
			if (res?.data) {
				const obj = {} as any
				[...res.data]?.map((item: any) => {
					const newList = item?.listDicts.map(
						(items: {value: any; valueName: any}) => ({
							dictKey: items.value.toString(),
							dictValue: items.valueName,
						}),
					)
					obj[item?.name] = newList
				})
				callback(obj)
			}
		})
		.catch((err) => {
			message.success(err)
		})
}
export default {
	getDict,
}
